package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManager {

	private static final String driver="com.mysql.jdbc.Driver";
	private static final String url = "jdbc:mysql://localhost:3306/exam";
	private static final String userName = "root";
	private static final String password = "root";
	
	protected static Connection conn = null;
	protected static Statement statement = null;
	protected static ResultSet rSet = null;
	
	
	public static Connection getConnection() {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url,userName,password);
		}catch(Exception e) {
			e.printStackTrace();
			System.out.println("Mysql连接失败");
		}
		return conn;
	}
	
	public static int executeUpdate(String sql) {
		int result = 0;
		try {
			statement = getConnection().createStatement();
			result = statement.executeUpdate(sql);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public void closed() {
		if(conn != null) {
			try {
				conn.close();
			}catch(SQLException e) {
				e.printStackTrace();
				System.out.println("Mysql关闭失败");
			}
		}
	}
}
